<template>
  <div>
    <!--面包屑导航栏-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <router-link to="/home/params">商品管理</router-link>
      </el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片区域-->
    <el-card>
      <!--警告区域-->
      <el-alert
          title="注意：只允许为第三级分类设置相关参数"
          type="warning"
          :closable="false"
      >
      </el-alert>
      <!--选择商品分类的区域-->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类:</span>
          <!--下方是连级选择器-->
          <el-cascader
              style="margin-left: 15px"
              v-model="CateKeys"
              :options="cateList"
              :props="{ expandTrigger: 'hover',value:'cat_id',label:'cat_name',children:'children' }"
              @change="handleChange"></el-cascader>
        </el-col>
      </el-row>
      <!--标签页区域-->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button :disabled="CateDisabled" type="primary" @click="showAdd">添加参数</el-button>
          <!--动态参数列表区域-->
          <el-table :data="manyList" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column label="#" type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" @click="SetParameter(scope.row.attr_id)">编辑</el-button>
                <el-button type="danger" @click="DeleteParameter(scope.row.attr_id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button :disabled="CateDisabled" type="primary" @click="showAdd">添加属性</el-button>
          <!--静态参数列表区域-->
          <el-table :data="onlyList" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column label="#" type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" @click="SetParameter(scope.row.attr_id)">编辑</el-button>
                <el-button type="danger" @click="DeleteParameter(scope.row.attr_id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!--添加对话框区域-->
    <el-dialog
        :title="addTitle"
        :visible.sync="AllAdd"
        width="50%"
        @close="addClose"
    >
      <el-form ref="AddForm" :rules="AddFormRules" :model="AddForm" label-width="80px">
        <el-form-item :label="addTitle" prop="attr_name">
          <el-input v-model="AddForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="AllAdd = false">取 消</el-button>
    <el-button type="primary" @click="Determine">确 定</el-button>
  </span>
    </el-dialog>
    <!--编辑对话框区域-->
    <el-dialog
        title="编辑"
        :visible.sync="SetDialogVisible"
        width="30%"
        @close="onSetClose"
    >
      <!--编辑对话框的表单区域-->
      <el-form ref="SetForm" :model="SetForm" :rules="SetFormRules" label-width="80px">
        <el-form-item :label=addTitle prop="attr_name">
          <el-input v-model="SetForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="SetDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="SetDown">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Params",
  data() {
    return {
      //这里通过接口获取的所有的商品分类列表
      cateList: [],
      //这是级联选择框里被选择的选项对应的id
      CateKeys: [],
      //被激活的页签的名称
      activeName: 'many',
      //这是静态参数的数据存储数组
      onlyList: [],
      //这是动态参数的数据存储数组
      manyList: [],
      //控制添加功能对话框的显示与隐藏
      AllAdd: false,
      //添加信息对话框里的表单数据源
      AddForm:
          {
            attr_name: ''
          },
      //添加表单的验证规则
      AddFormRules:
          {
            attr_name: [
              {required: true, message: '请输入属性名称', trigger: 'blur'},
            ]
          },
      //控制编辑对话框的显示和隐藏
      SetDialogVisible: false,
      //编辑对话框里的表单的数据源
      SetForm: {
        attr_name: ''
      },
      //编辑对话框里的表单的验证规则
      SetFormRules: {
        attr_name: [
          {required: true, message: '请输入属性名称', trigger: 'blur'},
        ]
      },
      //属性id
      attr_id: -1


    }
  },
  computed:
      {
        //控制标签页是否被禁用
        CateDisabled() {
          if (this.CateKeys.length !== 3) {
            return true
          }
          return false;
        },
        //获取当前的三级分类的id
        cateId() {
          if (this.CateKeys.length === 3) {
            return this.CateKeys[2]
          }
          return null
        },
        //动态的对话框提示标题
        addTitle() {
          return this.activeName === "only" ? "静态参数" : "动态参数";
        }
      },
  methods: {
    //获取所以的商品分类列表
    async getCateList() {
      const {data: res} = await this.$http.get('categories');
      if (res.meta.status !== 200) return this.$message.error('获取商品分类信息失败')
      this.cateList = res.data;
      this.$message.success('获取商品分类信息成功');
      console.log(this.cateList)
    },
    //这是级联选择框里数据发生变化时触发的回调函数
    async handleChange() {
      if (this.CateKeys.length !== 3) {
        this.CateKeys = [];
        return
      }
      await this.getCateOnly();

    },
    //页签的点击事件
    async handleClick() {
      console.log(this.activeName)
      await this.getCateOnly();
    },
    //专用的获取商品分类参数和属性的函数
    async getCateOnly() {
      const {data: res} = await this.$http.get(`categories/${this.cateId}/attributes`, {params: {sel: this.activeName}});
      if (res.meta.status !== 200) return this.$message.error('获取相关参数失败')
      this.$message.success('获取相关参数成功')
      if (this.activeName === 'only') {
        this.onlyList = res.data;
        console.log(this.onlyList)
      } else if (this.activeName === 'many') {
        this.manyList = res.data;
        console.log(this.manyList)
      }

    },
    showAdd() {
      this.AllAdd = true;
    },
    //点击添加对话框里的确定按钮
    Determine() {
      this.$refs.AddForm.validate(async (valid) => {
        if (!valid) return
        const {data: res} = await this.$http.post(`categories/${this.cateId}/attributes`, {
          attr_name: this.AddForm.attr_name,
          attr_sel: this.activeName,
        })
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error('添加属性失败')
        await this.getCateOnly();
        this.$message.success('添加属性成功')
      })

      this.AllAdd = false;
    },
    //对话框关闭时触发的回调
    addClose() {
      //官方提供的清空表单的api
      this.$refs.AddForm.resetFields();
    },
    //点击编辑按钮的回调函数
    SetParameter(id) {
      this.attr_id = id
      this.SetDialogVisible = true;
    },
    //点击删除按钮的回调函数
    DeleteParameter(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data: res} = await this.$http.delete(`categories/${this.cateId}/attributes/${id}`)
        if (res.meta.status !== 200) {
          this.$message.error('删除用户失败')
          return
        }
        await this.getCateOnly();
        this.$message({
          type: 'success',
          message: '删除成功!'
        });

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //编辑对话框关闭的回调
    onSetClose() {
      this.$refs.SetForm.resetFields();
    },
    //编辑对话框里的确定按钮
    SetDown() {
      this.$refs.SetForm.validate(async (valid) => {
        if (!valid) return
        const {data: res} = await this.$http.put(`categories/${this.cateId}/attributes/${this.attr_id}`, {
          attr_name: this.SetForm.attr_name,
          attr_sel: this.activeName
        })
        if (res.meta.status !== 200) return this.$message.error('更新失败');
        await this.getCateOnly();
        this.$message.success('更新成功');
        this.SetDialogVisible = false

      })
    }
  },
  created() {
    this.getCateList();
  }
}
</script>

<style lang="less" scoped>
.cat_opt {
  margin: 15px 0;
}
</style>
